package net.qhzw.vip.qiyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.qhzw.vip.qiyu.entity.DtsUser;
import net.qhzw.vip.qiyu.entity.GuaranteeCompany;
import net.qhzw.vip.qiyu.handler.MyException;
import net.qhzw.vip.qiyu.mapper.GuaranteeCompanyMapper;
import net.qhzw.vip.qiyu.model.GuaranteeCompanyUserVo;
import net.qhzw.vip.qiyu.result.ResultCode;
import net.qhzw.vip.qiyu.service.DtsUserService;
import net.qhzw.vip.qiyu.service.GuaranteeCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.Date;

@Service
public class GuaranteeCompanyServiceImpl extends ServiceImpl<GuaranteeCompanyMapper, GuaranteeCompany> implements GuaranteeCompanyService {


    @Autowired
    private DtsUserService dtsUserService;

    @Autowired
    private GuaranteeCompanyMapper guaranteeCompanyMapper;


    /***
     * 添加站点公司
     * @param guaranteeCompany
     * @return
     */
    @Override
    public boolean addGuaranteeCompany(GuaranteeCompany guaranteeCompany) {
        guaranteeCompany.setId(IdWorker.getId());
        guaranteeCompany.setStatus((byte) 1);
        guaranteeCompany.setCreated(new Date());
        return save(guaranteeCompany);
    }

    /***
     * 根据ID查询担保公司名称
     * @param companyId
     * @return
     */
    @Override
    public String getNameById(Long companyId) {
        GuaranteeCompany guaranteeCompany = getById(companyId);
        if (guaranteeCompany != null) {
            return guaranteeCompany.getName();
        }
        return null;
    }

    @Override
    public Page<GuaranteeCompany> findByPage(Page<GuaranteeCompany> page, Long shopId, String name, Byte status) {
        page.addOrder(OrderItem.desc("created"));
        Page<GuaranteeCompany> companyPage = page(page, new LambdaQueryWrapper<GuaranteeCompany>()
                .eq(shopId != 1, GuaranteeCompany::getShopId, shopId)
                .eq(!StringUtils.isEmpty(status), GuaranteeCompany::getStatus, status)
                .eq(!StringUtils.isEmpty(name), GuaranteeCompany::getName, name));
        return companyPage;
    }

    /***
     * 根据用户id查询担保公司
     * @param userId
     * @param status
     * @return
     */
    @Override
    public GuaranteeCompanyUserVo getGuaranteeCompanyByUserId(Long userId, Byte status) {
        DtsUser dtsUser = dtsUserService.getById(userId);
        if (dtsUser == null) {
            throw new MyException(ResultCode.PARAMERR);
        }
        GuaranteeCompanyUserVo guaranteeCompanyUserVo = guaranteeCompanyMapper.findGuaranteeCompanyByUserIdCard(dtsUser.getShopId(), dtsUser.getIdCard(), status);
        if (guaranteeCompanyUserVo == null) {
            guaranteeCompanyUserVo = new GuaranteeCompanyUserVo();
        }
        guaranteeCompanyUserVo.setRealName(dtsUser.getRealName());
        guaranteeCompanyUserVo.setMobile(dtsUser.getMobile());
        guaranteeCompanyUserVo.setIdCard(dtsUser.getIdCard());
        return guaranteeCompanyUserVo;
    }

    /***
     * 根据用户id查询担保公司
     * @param userId
     * @return
     */
    @Override
    public String getNameByUserId(Long userId) {
        DtsUser dtsUser = dtsUserService.getById(userId);
        if (dtsUser == null) {
            return null;
        }
        String idCard = dtsUser.getIdCard();
        //根据用户身份证号查询担保公司
        return guaranteeCompanyMapper.getNameByUserId(idCard);
    }


    @Override
    public GuaranteeCompany getById(Serializable id) {
        return getOne(new LambdaQueryWrapper<GuaranteeCompany>()
                .eq(id != null, GuaranteeCompany::getId, id)
                .eq(GuaranteeCompany::getStatus, 1));
    }
}
